package com.zeng.sim.controller;

import cn.hutool.core.collection.ListUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zeng.sim.controller.domain.SysConfig;
import com.zeng.sim.controller.domain.request.PageDomain;
import com.zeng.sim.controller.domain.response.Result;
import com.zeng.sim.controller.domain.response.ResultTable;
import com.zeng.sim.mapper.model.SysConfigEntity;
import com.zeng.sim.service.SysConfigService;
import com.zeng.sim.util.SimConstant;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zengwanlin
 * @since 2023-12-20
 */
@Controller
@RequestMapping("/sys/config")
public class SysConfigController {

    @Autowired
    private SysConfigService sysConfigService;

    @GetMapping("/data")
    @ResponseBody
    public ResultTable data(SysConfig sysConfig, PageDomain pageDomain) {
        Page<SysConfig> page = sysConfigService.pageQuery(sysConfig, pageDomain);
        return ResultTable.pageTable(page);
    }

    @GetMapping("/main")
    public String main() {
        return "system/config/main";
    }

    @GetMapping("/add")
    public String add() {
        return "system/config/add";
    }

    @GetMapping("/edit")
    public String edit(Model model, String configId) {
        SysConfigEntity entity = sysConfigService.getById(configId);
        SysConfig sysConfig = new SysConfig();
        BeanUtils.copyProperties(entity, sysConfig);
        model.addAttribute("sysConfig", sysConfig);
        return "system/config/edit";
    }

    @DeleteMapping("/remove/{configId}")
    @ResponseBody
    public Result remove(@PathVariable String configId) {
        LambdaQueryWrapper<SysConfigEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysConfigEntity::getConfigId, configId);
        wrapper.eq(SysConfigEntity::getConfigType, SimConstant.SYS_CONFIG_TYPE_SYSTEM);
        if (sysConfigService.exists(wrapper)) {
            throw new IllegalArgumentException("系统配置不允许删除");
        }
        return Result.decide(sysConfigService.removeById(configId));
    }

    @DeleteMapping("/batchRemove/{configIds}")
    @ResponseBody
    public Result batchRemove(@PathVariable String configIds) {
        List<Integer> idList = ListUtil.toList(configIds.split(","))
            .stream()
            .mapToInt(Integer::parseInt)
            .boxed()
            .collect(Collectors.toList());
        LambdaQueryWrapper<SysConfigEntity> wrapper = new LambdaQueryWrapper<>();
        for (Integer configId : idList) {
            wrapper.clear();
            wrapper.eq(SysConfigEntity::getConfigId, configId);
            wrapper.eq(SysConfigEntity::getConfigType, SimConstant.SYS_CONFIG_TYPE_SYSTEM);
            if (sysConfigService.exists(wrapper)) {
                throw new IllegalArgumentException("系统配置不允许删除");
            }
        }
        return Result.decide(sysConfigService.removeBatchByIds(idList));
    }

    @PutMapping("/update")
    @ResponseBody
    public Result update(@RequestBody SysConfig sysConfig) {
        return Result.decide(sysConfigService.updateSysConfig(sysConfig));
    }

    @PostMapping("/save")
    @ResponseBody
    public Result save(@RequestBody SysConfig sysConfig) {
        return Result.decide(sysConfigService.saveSysConfig(sysConfig));
    }

}
